package util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Vladimir
 * Date: 19.11.12 Time: 19:12
 */
public class FreeMakerSample {
    public static void main(String[] args) throws IOException, TemplateException {
        Configuration cfg = new Configuration();
        Template template = cfg.getTemplate("hello2.ftl");

        Map<String, Object> root = new HashMap<String, Object>();
        root.put("title", "Hello!");

        List<String> headers = new ArrayList<String>();
        headers.add("header #1");
        headers.add("header #2");
        headers.add("header #3");
        headers.add("header #...");
        root.put("headers", headers);


        Wheel wheel = new Wheel();
        wheel.name = "wheel";

        Map<String, Object> wheelMap = new HashMap<String, Object>();
        wheelMap.put("name", wheel.name);

        root.put("wheel", wheelMap);

        Car car = new Car();
        car.name = "car";

        Map<String, Object> carMap = new HashMap<String, Object>();
        carMap.put("name", car.name);

        root.put("car", carMap);

        List<Row> rows = new ArrayList<Row>();
        Row row = new Row();
        row.columns.add("row1 column1");
        row.columns.add("row1 column2");
        row.columns.add("row1 column3");
        row.columns.add("row1 column...");
        rows.add(row);

        row.columns.add("row2 column1");
        row.columns.add("row2 column2");
        row.columns.add("row2 column3");
        row.columns.add("row2 column...");
        rows.add(row);

//        Map<String, Object> columnsMap = new HashMap<String, Object>();
//        columnsMap.put("columns", row.columns);

        Map<String, Object> rowMap = new HashMap<String, Object>();
        rowMap.put("columns", row.columns);

//        .put("row.columns", columnsMap);
        root.put("rows", rows);
        root.put("row", rowMap);

        StringWriter sw = new StringWriter();
        template.process(root, sw);

        System.out.println(sw.getBuffer());

    }
/*
    public static void main(String[] args) throws IOException, TemplateException {
        Configuration cfg = new Configuration();
        Template template = cfg.getTemplate("hello2.ftl");

        Map<String, Object> data = new HashMap<String, Object>();
        List<String> headers = new ArrayList<String>();
        headers.add("header #1");
        headers.add("header #2");
        headers.add("header #3");
        headers.add("header #...");

        List<String> columns = new ArrayList<String>();
        headers.add("column #1");
        headers.add("column #2");
        headers.add("column #3");
        headers.add("column #...");

        data.put("title", "Hello!");
        data.put("headers", headers);

        Wheel wheel = new Wheel();
        wheel.name = "wheel name";
        Car car = new Car();
        car.name = "car name";
        car.wheel = wheel;

        data.put("wheel", wheel);
        data.put("wheel.name", wheel.name);
        data.put("car", car);
        data.put("car.name", car.name);

//        int rows = 5;
//        data.put("columns", columns);
//        List<Row> rows = new ArrayList<Row>();

//        Row row = new Row();
//        row.columns.add("row1 column1");
//        row.columns.add("row1 column2");
//        row.columns.add("row1 column3");
//        row.columns.add("row1 column...");

//        rows.add(row);
//        row.columns.add("row2 column1");
//        row.columns.add("row2 column2");
//        row.columns.add("row2 column3");
//        row.columns.add("row2 column...");
//        rows.add(row);

//        data.put("rows", rows);
//        data.put("row.columns", rows);


//
//        List<String> countries = new ArrayList<String>();
//        countries.add("India");
//        countries.add("United States");
//        countries.add("Germany");
//        countries.add("France");
//
//        data.put("countries", countries);
//
//
        StringWriter sw = new StringWriter();
        template.process(data, sw);
//
        System.out.println(sw.getBuffer());
    }
*/

}

class Row {
    public List<String> columns = new ArrayList<String>();

    public List<String> getColumns() {
        return columns;
    }

    public void setColumns(List<String> columns) {
        this.columns = columns;
    }
}

class Car {
    String name;
//    Wheel wheel;

}

class Wheel {
    String name;
}
